package com.lz.library.extension

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.lz.library.R

/**
 *  author: you : 2020/10/27
 */

fun <T : Activity> Activity.startActivity(cls: Class<T>) {
    startActivity(Intent(this, cls))
}

/**
 * 设置activity window alpha
 * @param alpha
 */
fun Activity.setWindowAlpha(alpha: Float) {
    window.attributes = window.attributes.also { it.alpha = alpha }
}

/**
 * 设置支持toolbar
 * @param toolbar
 * @param statusColor
 */
fun AppCompatActivity.supportToolbar(toolbar: Toolbar, @ColorInt statusColor: Int) {
    also { setSupportActionBar(toolbar) }.supportActionBar!!.setDisplayShowTitleEnabled(false)
}

/**
 * 设置透明的statusbar, 此时状态栏完全显示Toolbar, 需要给Toolbar设置PaddingTop
 * 特效颜色效果时只需要修改Toolbar的背影即可
 * @param toolbar
 */
fun Activity.setTranslucentStatusBar(toolbar: Toolbar) {
    window.apply {
        clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        statusBarColor = Color.TRANSPARENT
    }
    if (toolbar.getTag(R.id.lib_toolbar_padding_tag) == null) {
        val statusHeight = getStatusHeight()
        toolbar.apply {
            layoutParams = layoutParams.apply { height += statusHeight }
            setPadding(paddingLeft, paddingTop + statusHeight, paddingRight, paddingBottom)
            setTag(R.id.lib_toolbar_padding_tag, true)
        }
    }
}

/**
 * 白色样式时改变顶部状态栏字体颜色
 */
fun Activity.whiteStatusBar() {
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}